package com.classic.core.utils;

import android.animation.ValueAnimator;
import android.widget.TextView;

import java.text.NumberFormat;

/**
 * Created by Li.z on 2016/5/1.
 */
public class NumberTextAnimation {

    /**递增显示数值
     * @param textView  要显示的控件
     * @param f 要显示的值
     * @param decimalNum 保留几位小数
     * @param format 执行String.format(format,显示的值); 例如 ："%s分钟"
     */
    public static void runFloat(final TextView textView,float f,int decimalNum, final String format){
        ValueAnimator progressAnimator = ValueAnimator.ofFloat(0, f);
        progressAnimator.setDuration(1000);

        final NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(decimalNum);
        progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            float tempF;
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                tempF= (float) animation.getAnimatedValue();
                textView.setText(String.format(format,nf.format(tempF)));
            }
        });
        progressAnimator.start();
    }

    public static void runFloat(final TextView textView,float f,int decimalNum){
        runFloat(textView,f,decimalNum,"%s");
    }



    public static void runInt(int i, final TextView textView){
        ValueAnimator progressAnimator = ValueAnimator.ofInt(0, i);
        progressAnimator.setDuration(1000);
        progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            int tempF;
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                tempF= (int) animation.getAnimatedValue();
                textView.setText(tempF+"");
            }
        });
        progressAnimator.start();
    }

}
